using Prism.Commands;
using Prism.Mvvm;

namespace FolderCompare.ViewModels;

public class WinTextCompareViewModel : BindableBase
{
    private IAppSettingsBase _settings => IocManage.Settings;
    public int FontSize => _settings.FontSize;
    public string FontName => _settings.FontName;
    public string Title => "文本比较";

    public string? OldText { get; set; }
    public string? NewText { get; set; }

    public DelegateCommand SubmitCommand { get; }

    public WinTextCompareViewModel(string? oldFile, string? newFile)
    {
        if (oldFile.FileExists())
            OldText = ReadAllText(oldFile);
        if (newFile.FileExists())
            NewText = ReadAllText(newFile);
        SubmitCommand = new DelegateCommand(OnSubmit);
    }

    private string ReadAllText(string fileName)
    {
        Encoding encoding = TextCodeGuessHelper.GuessFileEncoding(fileName);
        if (encoding == null)
        {
            return File.ReadAllText(fileName);
        }
        else
        {
            return File.ReadAllText(fileName, encoding);
        }
    }

    private void OnSubmit()
    {
        ApplicationBase.GetActiveWindow().DialogResult = true;
    }
}